package com.bqtt.tumor.demo.presentation.interceptor;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.bqtt.tumor.demo.common.utility.InlineTools;
import com.bqtt.tumor.demo.entity.BodyPart;
import com.bqtt.tumor.demo.entity.City;
import com.bqtt.tumor.demo.entity.Desease;
import com.bqtt.tumor.demo.entity.DiagnosisMethod;
import com.bqtt.tumor.demo.entity.Feature;
import com.bqtt.tumor.demo.entity.HealthUser;
import com.bqtt.tumor.demo.entity.Hospital;
import com.bqtt.tumor.demo.entity.Image;
import com.bqtt.tumor.demo.entity.LabSheet;
import com.bqtt.tumor.demo.entity.LabSheetResult;
import com.bqtt.tumor.demo.entity.MedicalCase;
import com.bqtt.tumor.demo.entity.Medicine;
import com.bqtt.tumor.demo.entity.Occupation;
import com.bqtt.tumor.demo.entity.SystemUser;
import com.bqtt.tumor.demo.entity.TreatmentMethod;
import com.bqtt.tumor.demo.entity.TreatmentPlan;
import com.bqtt.tumor.demo.entity.TreatmentTrackHistory;


/**
 * @author tangb
 *
 */
@Repository
public class SessionInterceptor extends HandlerInterceptorAdapter {


	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle
	 * (javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		
		if(InlineTools.imgWebPathDirectory.isEmpty())
			
			InlineTools.imgWebPathDirectory =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ request.getContextPath()+"/resources/medical_images/";
	
		SystemUser currentUser = (SystemUser) request.getSession().getAttribute("currentUser");
		Object historySearchCondition = request.getSession().getAttribute("historySearchCondition");
	    if(currentUser==null)
	    {
	    	currentUser = new SystemUser();
	    	currentUser.setSessionState(0);
	    	request.getSession().setAttribute("currentUser", currentUser);
	    }
		if(historySearchCondition==null)
		{
			if(request.getRequestURI().contains("HealthUser"))
			{
				historySearchCondition = new HealthUser();
			}
			else if(request.getRequestURI().contains("MedicalCase"))
			{
				historySearchCondition = new MedicalCase();
			}
			else if(request.getRequestURI().contains("City"))
			{
				historySearchCondition = new City();
			}
			else if(request.getRequestURI().contains("BodyPart"))
			{
				historySearchCondition = new BodyPart();
			}
			else if(request.getRequestURI().contains("Desease"))
			{
				historySearchCondition = new Desease();
			}
			else if(request.getRequestURI().contains("DiagnosisMethod"))
			{
				historySearchCondition = new DiagnosisMethod();
			}
			else if(request.getRequestURI().contains("Feature"))
			{
				historySearchCondition = new Feature();
			}
			else if(request.getRequestURI().contains("Hospital"))
			{
				historySearchCondition = new Hospital();
			}
			else if(request.getRequestURI().contains("LabSheet"))
			{
				historySearchCondition = new LabSheet();
			}
			else if(request.getRequestURI().contains("LabSheetResult"))
			{
				historySearchCondition = new LabSheetResult();
			}
			else if(request.getRequestURI().contains("Medicine"))
			{
				historySearchCondition = new Medicine();
			}
			else if(request.getRequestURI().contains("Occupation"))
			{
				historySearchCondition = new Occupation();
			}
			else if(request.getRequestURI().contains("TreatmentMethod"))
			{
				historySearchCondition = new TreatmentMethod();
			}
			else if(request.getRequestURI().contains("TreatmentPlan"))
			{
				historySearchCondition = new TreatmentPlan();
			}
			else if(request.getRequestURI().contains("TreatmentTrackHistory"))
			{
				historySearchCondition = new TreatmentTrackHistory();
			}
			else if(request.getRequestURI().contains("Image"))
			{
				historySearchCondition = new Image();
			}
			else
			{
				historySearchCondition =null;
			}
			
			request.getSession().setAttribute("historySearchCondition", historySearchCondition);
		}
	    
		return super.preHandle(request, response, handler);
	}

}
